#include <algorithm>
#include <cstdio>
#include <cstring>
#define INF 1e8
using namespace std;

int pri_gcd, pri_lcm, pri_a0, pri_b0, pri[10000];
int a0, gcd, b0, lcm, n, ans, flag, cnt;
int l1, l2, r1, r2;
bool v[46000];
int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen("son.in", "r", stdin);
	freopen("son.out", "w", stdout);
#endif
#endif
	scanf("%d", &n);
	for (int i = 2; i <= 46000; i++)
		if (!v[i]) {
			pri[++cnt] = i;
			for (int j = 2; j * i <= 46000; j++)
				v[j * i] = 1;
		}
	while (n--) {
		ans = 1;
		flag = true;
		scanf("%d %d %d %d", &a0, &gcd, &b0, &lcm);
		for (int i = 1; i < cnt; ++i) {
			pri_a0 = pri_b0 = pri_lcm = pri_gcd = 0;
			while (a0 % pri[i] == 0) {
				pri_a0++;
				a0 /= pri[i];
			}
			while (gcd % pri[i] == 0) {
				pri_gcd++;
				gcd /= pri[i];
			}
			while (b0 % pri[i] == 0) {
				pri_b0++;
				b0 /= pri[i];
			}
			while (lcm % pri[i] == 0) {
				pri_lcm++;
				lcm /= pri[i];
			}
			l1 = 0, l2 = 0;
			r1 = INF, r2 = INF;
			if (pri_a0 == pri_gcd) {
				l1 = pri_gcd;
			} else {
				l1 = r1 = pri_gcd;
			}
			if (pri_b0 == pri_lcm) {
				r2 = pri_lcm;
			} else {
				l2 = r2 = pri_lcm;
			}
			if (l1 > r2 || l2 > r1) {
				flag = false;
				break;
			}
			ans *= min(r1, r2) - max(l1, l2) + 1;
		}
		if (b0 == lcm && b0 != 1) ans *= 2;
		if (flag)
			printf("%d\n", ans);
		else
			printf("0\n");
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}